<?php include "include/header.php" ?>
<div class="main-content">
					<div class="container">
										
						<div class="page-content">
						
							<!-- Heading -->
							<div class="single-head">
								<!-- Heading -->
								<h3 class="pull-left"><i class="fa fa-tags green"></i> Report Item</h3>
								<!-- Bread crumbs -->
								<div class="breads pull-right">
									<strong>Nav</strong> : <a href="<?=base_url('/')?>">Home</a> / <a href="<?=base_url('report_item')?>">Report Item</a>
								</div>
								<div class="clearfix"></div>
							</div>
							
							<!-- Posts page -->
							<div class="page-posts">
								<!-- Nav tabs -->
								<div class="page-tabs">
									<ul class="nav nav-tabs">
										<li class="active"><a href="#posts" class="br-lblue" data-toggle="tab"><i class="fa fa-tags lblue"></i> Report Item</a></li>
										<li><a href="#postnew" class="br-red" data-toggle="tab"><i class="fa fa-tag red"></i> Add Report Item</a></li>
									</ul>
									<!-- Tab panes -->
									<div class="tab-content">
										<div class="tab-pane fade active in" id="posts">
											<form id="searchForm">
												<div class="col-md-2">
													<select name="report_id" id="report_id" class="form-control">
														<option>Select Report Name</option>
														<?php foreach ( $reports as $row ) { ?>
															<option value="<?=$row['report_id']?>"><?=$row['report_name']?></option>
														<?php } ?>
													</select>
												</div>
												<div class="group_lists"></div>
												<div class="col-md-1">
													<input type="submit" id="search_items" class="form-control btn btn-default btn-lg" value="Search">
												</div>
											</form>
											<div class="clearfix"></div><br>
											<div class="table-responsive">
												<form id="item_form">
													<table class="table table-bordered">
														<thead>
															<tr>
																<th><input type='checkbox' onclick="var ck = this.checked;$(':checkbox').each(function(){this.checked = ck});"/></th>
															
																<th>Group name</th>
																<th>Item name</th>
																<th>Item key</th>
																<th>Item type</th>
																<th>Item server</th>
																<th>Item sort</th>
																<th>Edits</th>
															</tr>
														</thead>
														<tbody>														
															<?php foreach ( $items as $row ) { ?>
																<tr>
																	<td class="ii"><input type='checkbox' name="item_id" value="<?=$row['item_id']?>" /></td>
																	
																	<td class="gn"><?=$row['group_name']?></td>
																	<td class="in"><?=$row['item_name']?></td>
																	<td class="ik"><?=$row['item_key']?></td>
																	<td class="it"><?=$row['item_type']?></td>
																	<td class="iv"><?=$row['item_server']?></td>
																	<td class="is"><?=$row['item_sort']?></td>
																	<td>
																		<a data-toggle="modal" href="#report_item_update" class="btn btn-xs btn-danger report_item_edit"><i class="fa fa-pencil"></i></a>																		
																		<a class="btn btn-xs btn-success item_del" onclick="confirm('Are you sure to delete?',function(flag){if(flag){location.href='<?=base_url('report_item/delete')?>?item_id=<?=$row['item_id']?>'}})" href="javascript:;">
																		<i class="fa fa-trash-o"></i> </a>
																	</td>
																</tr>
															<?php } ?>
														</tbody>
													</table>
												</form>
											</div><!-- Table responsive end -->
										<!--
											<ul class="pagination pull-right">
												<li><a href="#">&laquo;</a></li>
												<li><a href="#">1</a></li>
												<li><a href="#">2</a></li>
												<li><a href="#">3</a></li>
												<li><a href="#">4</a></li>
												<li><a href="#">5</a></li>
												<li><a href="#">&raquo;</a></li>
											</ul>-->
											<div class="clearfix"></div>
										</div>
										<div class="tab-pane fade" id="postnew">
											<!-- Heading -->
											<h4>Add Report Item</h4>	
											<form class="form-horizontal" role="form"  method="post" action="<?=base_url('report_item/add')?>">
												<!-- Report Name area -->
												<div class="form-group">
													<label for="report_name" class="control-label col-md-2">Group Name</label>
													<div class="col-md-10">
														<select id="location" name="group_name" class="form-control">
															<option value="">Select Group Name</option>
															<?php foreach ( $groups as $row ) { ?>
																<option value="<?=$row['group_name']?>"><?=$row['group_name']?></option>
															<?php } ?>
														</select>
													</div>
												</div>
												<!-- Itemt Key area -->
												<div class="form-group">
													<label for="tag" class="col-md-2 control-label">Item Key</label>
													<div class="col-md-10">
														<input type="text" name="item_key" class="form-control" id="title" placeholder="Enter Item Key">
													</div>
												</div>
												<!-- Itemt name area -->
												<div class="form-group">
													<label for="tag" class="col-md-2 control-label">Item Name</label>
													<div class="col-md-10">
														<input type="text" name="item_name" class="form-control" id="title" placeholder="Enter Item Name">
													</div>
												</div>
												<div class="form-group">
													<label for="tag" class="col-md-2 control-label">Item Type</label>
													<div class="col-md-10">
														<input type="text" name="item_type" class="form-control" id="title" placeholder="Enter Item Type">
													</div>
												</div>
												<div class="form-group">
													<label for="tag" class="col-md-2 control-label">Item Server</label>
													<div class="col-md-10">
														<input type="text" name="item_server" class="form-control" id="title" placeholder="Enter Item Server">
													</div>
												</div>
												<!--sort area-->
												<div class="form-group">
													<label for="tag" class="col-md-2 control-label">Item Sort</label>
													<div class="col-md-10">
														<input type="text" name="item_sort" class="form-control" id="title" placeholder="Enter Item Sort">
													</div>
												</div>
												<!-- Button -->
												<div class="form-group">
													<div class="col-md-offset-2 col-md-10">
													<div class="pull-right">
														<input type="submit" class="btn btn-default" value="Add" />
													</div>
													<div class="clearfix"></div>
													</div>
												</div>
											</form>
											<h4>Batch Adding</h4>
											<form class="form-horizontal" role="form"  method="post" action="<?=base_url('report_item/batch_add')?>">
												<div class="form-group">
													<div class="col-md-12">
														<textarea class="textarea" name="items" class="form-control" placeholder=" group name : item key : item name : item type : item server : item sort" style="width: 100%; height: 200px"></textarea>
													</div>
												</div>
												<div class="form-group">
													<div class="col-md-10">
														<div class="pull-left">
															<input type="submit" class="btn btn-default" value="Submit" />
														</div>
														<div class="clearfix"></div>
													</div>
												</div>
											</form>
											<!--End batch adding-->
										</div>
									</div>
								</div>
							</div> <!-- Posts page end -->
							<!-- report update form -->
							<div class="modal fade" id="report_item_update" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
								<div class="modal-dialog">
								    <div class="modal-content">    
					 			        <div class="modal-header">
									        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
										        <h4 class="modal-title" id="exampleModalLabel">Update Report Item</h4>
										</div>
									    <div class="modal-body">
										    <form class="form-horizontal" role="form" method="post" action="<?=base_url('report_item/update')?>">
										    	<input type="checkbox" name="item_id" id="item_id" checked="checked" hidden>
										        <div class="form-group">
													<label for="report_name" class="control-label col-md-2">Group Name</label>
													<div class="col-md-10">
														<select id="group_name" name="group_name" class="form-control">
															<option value="">Select Group Name</option>
															<?php foreach ( $groups as $row ) { ?>
																<option value="<?=$row['group_name']?>"><?=$row['group_name']?></option>
															<?php } ?>
														</select>
													</div>
												</div>
												<!-- Itemt Key area -->
												<div class="form-group">
													<label for="tag" class="col-md-2 control-label">Item Key</label>
													<div class="col-md-10">
														<input type="text" name="item_key" class="form-control" id="item_key" placeholder="Enter Item Key">
													</div>
												</div>
												<!-- Itemt name area -->
												<div class="form-group">
													<label for="tag" class="col-md-2 control-label">Item Name</label>
													<div class="col-md-10">
														<input type="text" name="item_name" class="form-control" id="item_name" placeholder="Enter Item Name">
													</div>
												</div>
												<div class="form-group">
													<label for="tag" class="col-md-2 control-label">Item Type</label>
													<div class="col-md-10">
														<input type="text" name="item_type" class="form-control" id="item_type" placeholder="Enter Item Type">
													</div>
												</div>
												<div class="form-group">
													<label for="tag" class="col-md-2 control-label">Item Server</label>
													<div class="col-md-10">
														<input type="text" name="item_server" class="form-control" id="item_server" placeholder="Enter Item Server">
													</div>
												</div>
												<!--sort area-->
												<div class="form-group">
													<label for="tag" class="col-md-2 control-label">Item Sort</label>
													<div class="col-md-10">
														<input type="text" name="item_sort" class="form-control" id="item_sort" placeholder="Enter Item Sort">
													</div>
												</div>
												<button type="submit" class="btn btn-info btn-block">Update</button>
										    </form>
										</div>
										<div class="clearfix"></div>
										<div class="modal-footer">
										    
									    </div>
								    </div>
						        </div>
							</div>
							<!-- report update form -->
						</div>
						
					</div>
				</div>
				</div>
			<!-- Mainbar ends -->
			
			<div class="clearfix"></div>
		</div>
<?php include "include/js.php" ?>
<script>
$(document).ready(function(){
	
	$("#report_item_update").on('show.bs.modal', function (event) {
		var button = $(event.relatedTarget);
		var item_id = button.parent().prevAll(".ii").children().val();
		var item_name = button.parent().prevAll(".in").text();
		var item_key = button.parent().prevAll(".ik").text();
		var item_type = button.parent().prevAll(".it").text();
		var item_server = button.parent().prevAll(".iv").text();
		var item_sort = button.parent().prevAll(".is").text();
		var group_name = button.parent().prevAll(".gn").text();
		var modal = $(this);
		modal.find('#item_id').val(item_id);
  		modal.find('#item_name').val(item_name);
  		modal.find('#item_key').val(item_key);
  		modal.find('#item_type').val(item_type);
  		modal.find('#item_server').val(item_server);   
  		modal.find('#item_sort').val(item_sort);
  		modal.find('#group_name').val(group_name);
	});
	$("#report_id").change(function(){
    	$(".group_lists").html("");
    	var reportId=$(this).val(); 
    	var groupName=<?php echo json_encode($group_names_list);?>;
    	$(".group_lists").append("<div class='col-md-2'><select name='group_id' id='group_id' class='form-control'></select></div>");
    	for(var i=0;i<groupName.length;i++){ 
    		for(var key in groupName[i]) { 
    			if(key==="report_id" && groupName[i][key]===reportId)
    			$("#group_id").append("<option value='"+groupName[i]['group_id']+"'>"+groupName[i]['group_name']+"</option>");
    		}
    	}
    });
    $('#searchForm').ajaxForm({
        beforeSubmit:function(){if ($('#group_id').val() =='' || $('#group_id').val() == undefined) {
                       alert("Not select report name or group name");}
                     },
        type: 'post',
        url: "<?=base_url('report_item/select_by_groupid')?>",
        dataType: 'json',
        success: function(data){
        	var items = eval(data);
        	$("tbody").html(""); 
        	for(var o in data){
        		$("tbody").append("<tr><td class='ii'><input type='checkbox' name='item_id' value='"+data[o].item_id+"' /></td><td class='gn'>"+data[o].group_name+"</td><td class='in'>"+data[o].item_name+"</td><td class='ik'>"+data[o].item_key+"</td><td class='it'>"+data[o].item_type+"</td><td class='iv'>"+data[o].item_server+"</td><td class='is'>"+data[o].item_sort+"</td><td><a data-toggle='modal' href='#report_item_update' class='btn btn-xs btn-danger report_item_edit'><i class='fa fa-pencil'></i></a> <a class='btn btn-xs btn-success item_del' onclick=\"confirm('Are you sure to delete?',function(flag){if(flag){location.href='<?=base_url('report_item/delete')?>?item_id="+data[o].item_id+"'}})\" href='javascript:;'><i class='fa fa-trash-o'></i> </a></td></tr>"); 	 
      		}
      		
        }
    });	
});
</script>
<?php include "include/footer.php" ?>